/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.BankDto;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author HOATNM
 */
public class BankDaoImpl extends BaseDao {

    // get all Bank from database and load to List
    public ArrayList<BankDto> getListBank() {
        ArrayList<BankDto> list = new ArrayList<BankDto>();
        if (getConnect()) {
            String sql = "SELECT * FROM Bank Where BankId!=1";
            try {
                preparedStatement = connection.prepareStatement(sql);
                resultSet = preparedStatement.executeQuery();

                if (resultSet != null) {
                    while (resultSet.next()) {
                        BankDto bank = new BankDto();
                        bank.setBankID(resultSet.getInt("BankID"));
                        bank.setBankName(resultSet.getString("BankName"));
                        bank.setDescribe(resultSet.getString("Describe"));
                        list.add(bank);
                    }
                }
            } catch (SQLException ex) {
                Logger.getLogger(EmployeeDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        disconnect();
        return list;
    }
    // delete bank via bankId

    public boolean deleteBank(int bankId) {
        // 
        boolean result = true;
        StringBuilder sqlAccount = new StringBuilder("UPDATE Account set BankID = ? where BankID= ? ");
        if (getConnect()) {
            try {
                int index = 1;
                preparedStatement = connection.prepareStatement(sqlAccount.toString());
                preparedStatement.setInt(index++, 1);
                preparedStatement.setInt(index++, bankId);
                preparedStatement.execute();
                

            } catch (SQLException ex) {
                result = false;
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
            }
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////


            StringBuilder sql = new StringBuilder("DELETE Bank WHERE bankID =?");

            try {
                int index = 1;
                preparedStatement = connection.prepareStatement(sql.toString());
                preparedStatement.setInt(index++, bankId);
                preparedStatement.executeUpdate();
                
            } catch (SQLException ex) {
                result = false;
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
            }

        }
        return result;
    }
    // insert a bank to database

    public boolean insertBank(BankDto dto) {
        boolean result = true;

        if (getConnect()) {
            StringBuilder sql = new StringBuilder("INSERT INTO bank VALUES (?,?)");
            try {
                int index = 1;
                preparedStatement = connection.prepareStatement(sql.toString());
                preparedStatement.setString(index++, dto.getBankName());
                preparedStatement.setString(index++, dto.getDescribe());
                int flag=0;
                flag= preparedStatement.executeUpdate();
                if(flag==0){
                    result=false;
                }

            } catch (SQLException ex) {
                result = false;
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
            }

        }
        disconnect();
        return result;
    }
    // update a bank

    public boolean updateBank(BankDto dto) {
        boolean result = true;

        if (getConnect()) {
            StringBuilder sql = new StringBuilder("UPDATE Bank set BankName = ?, Describe = ? where BankID= ? ");
            try {
                int index = 1;
                preparedStatement = connection.prepareStatement(sql.toString());
                preparedStatement.setString(index++, dto.getBankName());
                preparedStatement.setString(index++, dto.getDescribe());
                preparedStatement.setInt(index++, dto.getBankID());
                int flag = 0;
                flag = preparedStatement.executeUpdate();
                if (flag == 0) {
                    result = false;
                }

            } catch (SQLException ex) {
                result = false;
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.WARNING_MESSAGE);
            }

        }
        disconnect();
        return result;
    }
}
